package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;

import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IMediaListByRegionIdView;

import com.cloudsigns.factorymanager2.engineering.adapter.MediaAdapter;

import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;

import com.cloudsigns.factorymanager2.engineering.bean.RegionBean;
import com.cloudsigns.factorymanager2.engineering.presenter.MediaListByIdPresenter;
import com.cloudsigns.factorymanager2.project.myinerface.ItemCheckedChangeListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tony on 2018/5/5.
 */

public class SelectMeidaForSuperActivity extends Activity implements View.OnClickListener, ItemCheckedChangeListener, IMediaListByRegionIdView {
    private View rl_back;
    private TextView tv_title;
    private TextView tv_airport_name;
    private ScrollView scrollview;
    private LinearLayout ll_region;
    private TextView btn_save;
    private LayoutInflater layoutInflater;
    List<MediaBean> selectedMediaBeanList = new ArrayList<>();
    List<String> selectedIdList = new ArrayList<>();
    Map<String, List<MediaBean>> pointBeanMap = new HashMap<>();
    private String TAG = SelectMeidaForSuperActivity.class.getSimpleName();
    private MediaListByIdPresenter mediaListByIdPresenter;
    private RegionBean selectedRegionBean;
    CommonEnum.EngineeringORderCategory engineeringORderCategory;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_add_point_location_list);

        mediaListByIdPresenter = new MediaListByIdPresenter(this);
        init();
        layoutInflater = getLayoutInflater();


        Intent mIntent = getIntent();
        if (mIntent != null) {
//            id = mIntent.getStringExtra(ConstantUtil.KEY_2);
//            String regionName = mIntent.getStringExtra("regionName");

            selectedIdList = (List<String>) mIntent.getSerializableExtra(ConstantUtil.KEY_1);
            selectedRegionBean = (RegionBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);
            engineeringORderCategory = (CommonEnum.EngineeringORderCategory) mIntent.getSerializableExtra("EngineeringORderCategory");

            if (null != selectedRegionBean)
                tv_airport_name.setText(selectedRegionBean.getRegionName());


        }
        mediaListByIdPresenter.getMediaListById();


    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_airport_name = findViewById(R.id.tv_airport_name);
        scrollview = findViewById(R.id.scrollview);
        ll_region = findViewById(R.id.ll_region);
        btn_save = findViewById(R.id.btn_save);

        tv_title.setText("点位信息");
        rl_back.setOnClickListener(this);
        btn_save.setOnClickListener(this);
    }

    private void initData(List<MediaBean> mediaRegionBeanList) {
        if (null != mediaRegionBeanList) {
            for (MediaBean mediaRegionBean : mediaRegionBeanList) {
                String areaName = mediaRegionBean.getAreaName();

                if (pointBeanMap.containsKey(areaName)) {
                    pointBeanMap.get(areaName).add(mediaRegionBean);
                } else {
                    List<MediaBean> beanList = new ArrayList<>();
                    beanList.add(mediaRegionBean);
                    pointBeanMap.put(areaName, beanList);

                }
            }
        }

        updateUi();

    }

    private void updateUi() {

        if (null != pointBeanMap && null != ll_region) {

            for (Map.Entry<String, List<MediaBean>> entry : pointBeanMap.entrySet()) {
                View view = layoutInflater.inflate(R.layout.layout_point_location_content, null);
                TextView tv_area_name = view.findViewById(R.id.tv_area_name);
                tv_area_name.setText(entry.getKey());

                ListView lv_media = view.findViewById(R.id.lv_media);

                List<MediaBean> mediaRegionBeanList = entry.getValue();
                if (null != mediaRegionBeanList) {
                    for (MediaBean mediaRegionBean : mediaRegionBeanList) {
                        String id = mediaRegionBean.getId();
                        if (selectedIdList.contains(id))
                            mediaRegionBean.setChecked(true);
                        else
                            mediaRegionBean.setChecked(false);

                    }
                }
                MediaAdapter adapter = new MediaAdapter(this, TAG);
                adapter.setData(mediaRegionBeanList);
                adapter.setItemCheckedChangeListener(this);
                lv_media.setAdapter(adapter);
                view.setTag(mediaRegionBeanList);


                ll_region.addView(view);


            }
        }

        scrollview.smoothScrollTo(0, 0);


    }


    private void save() {
        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.KEY_1, (Serializable) selectedIdList);
        mIntent.putExtra(ConstantUtil.KEY_2, (Serializable) selectedMediaBeanList);
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
        Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();

        finish();
    }

    @Override
    public void checkedChangeListener(View view, int position, boolean isSlected) {
        MediaBean bean = (MediaBean) view.getTag();
        String id = bean.getId();


//        String posStr = position + "";
        if (isSlected) {
            if (!selectedIdList.contains(id)) {
                selectedIdList.add(id);
                selectedMediaBeanList.add(bean);
            }
        } else {
            if (selectedIdList.contains(id)) {
                selectedIdList.remove(id);
                selectedMediaBeanList.remove(bean);
            }
        }


    }

    @Override
    public void checkedChangeListener(int position, boolean isSlected) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.btn_save:
                save();
                break;
        }
    }

    @Override
    public String getId() {
        if (null != selectedRegionBean)
            return selectedRegionBean.getId();

        return "";
    }

    @Override
    public CommonEnum.EngineeringORderCategory getEngineeringDeptOrderCategoryEnum() {
        return engineeringORderCategory;
    }

    @Override
    public void getMediaListByIdSuccessed(List<MediaBean> mediaRegionBeanList) {
        initData(mediaRegionBeanList);
    }

    @Override
    public void showFailedError(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
